<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use app\admin\model\NodeModel;
use app\admin\model\WebsetModel;
class Base extends Controller
{
    public function _initialize()
    {
        if(empty(session('username'))){

            $loginUrl = url('login/index');
            if(request()->isAjax()){
                return msg(111, $loginUrl, '登录超时');
            }

            $this->redirect($loginUrl);
        }

        // 检测权限
        $control = lcfirst(strtolower(request()->controller()));
        $action = lcfirst(strtolower(request()->action()));

        if(empty(authCheck($control . '/' . $action))){
            if(request()->isAjax()){
                return msg(403, '', '您没有权限');
            }

            $this->error('403 您没有权限');
        }

        $this->assign([
            'head'     => session('head'),
            'username' => session('username'),
            'rolename' => session('role')
        ]);

		$node = new NodeModel();
        $this->assign([
            'menu' => $node->getMenu(session('rule'))
        ]);

		$webset = new WebsetModel();
		$this->assign([
			'Webset'=>$webset->getInfo(),
            'user_id'=>session('id')
		]);

        // 记录操作日志
        $operationController = strtolower($this->request->controller());
        $operationAction = strtolower($this->request->action());
        $operationUrl = $operationController . "/" . $operationAction;
        $operationName = DB::name('node')->where([
            'control_name'=>$operationController,
            'action_name'=>$operationAction
        ])->value('node_name');
        if($operationName == ''){
            $operationName = '首页';
        }
        $logData = [
            'c_time' => time(),
            'url' => $operationUrl,
            'url_name' => $operationName,
            'u_id' => session('id'),
            'user_name' => session('username')
        ];
        DB::name('log')->insert($logData);
    }
}
